package com.hdcx.Minfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.fragment.ClassifyFragment;
import com.hdcx.Minfresh.fragment.MainFragment;
import com.hdcx.Minfresh.fragment.MineFragment;
import com.hdcx.Minfresh.fragment.ShoppingCartFragment;
import com.hdcx.Minfresh.url.RequstCode;
import com.hdcx.Minfresh.util.AppUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    private int index;//fragmentList的下标，当前片段的位置
    public TextView tv_tab;
    public TextView tv_ta01;
    private TextView mine_tab;
    public TextView classify;
    private TextView od_tab;
    private TextView cz_tab;
    public List<String > tag_list = new ArrayList<>();
    public LinearLayout ly;
    public Boolean isFromMainFragmentToClassify =false;
    public String id ;
    public TextView cart_num;//购物车数目


    private static MainFragment mainFragment;
    private static ClassifyFragment classifyFragment;
    private static ShoppingCartFragment shoppingCartFragment;
    private static MineFragment mineFragment;
    public LinearLayout ly_bottom_bar;
    private boolean isSelect;
    private FragmentManager fManager;
    private FragmentTransaction mTransaction;

    public boolean isSelect() {
        return isSelect;
    }
    public void setIsSelect(boolean isSelect) {
        this.isSelect = isSelect;
    }
    private boolean close;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        fManager = getSupportFragmentManager();
        super.onCreate(savedInstanceState);
       /* getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);*/
        setContentView(R.layout.activity_main);
        ly = (LinearLayout) findViewById(R.id.ly_bottom_bar);
        if (savedInstanceState != null) {
            inItViewFragment(true);
        }else{
            inItViewFragment(false);
        }
        inItView();
        String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();
        Log.e("LoginActivity", "LoginActivity--post_login--"+deviceId);

    }

    private void inItViewFragment(boolean b) {
        tag_list.add("Main");
        tag_list.add("classify");
        tag_list.add("cart");
        tag_list.add("mine");
        if(b){
            mainFragment = (MainFragment) fManager.findFragmentByTag("Main");
            fragmentList.add(mainFragment);
            classifyFragment = (ClassifyFragment) fManager.findFragmentByTag("classify");
            fragmentList.add(classifyFragment);
            shoppingCartFragment = (ShoppingCartFragment) fManager.findFragmentByTag("cart");
            fragmentList.add(shoppingCartFragment);
            mineFragment = (MineFragment) fManager.findFragmentByTag("mine");
            fragmentList.add(mineFragment);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.hide(classifyFragment).hide(mineFragment).hide(shoppingCartFragment).show(mainFragment);
        }else{
            // 首页
            mainFragment = new MainFragment();
            //分类
            classifyFragment = new ClassifyFragment();
            // 购物车
            shoppingCartFragment = new ShoppingCartFragment();
            // 我的
            mineFragment = new MineFragment();

            fragmentList.add(mainFragment);
            fragmentList.add(classifyFragment);
            fragmentList.add(shoppingCartFragment);
            fragmentList.add(mineFragment);
            /**
             * 默认显示首页片段
             */

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.content, mainFragment, "Main");
            fragmentTransaction.commitAllowingStateLoss();
            index = 0;//初始化选中0
        }
    }


    public void inItView() {
        tv_tab = (TextView) findViewById(R.id.tv_tab01);
        tv_ta01 = (TextView) findViewById(R.id.tv_tab01);
        od_tab = (TextView) findViewById(R.id.tv_tab03);
        tv_tab.setSelected(true);
        classify = (TextView) findViewById(R.id.tv_tab02);
        cz_tab = (TextView) findViewById(R.id.tv_tab04);
        ly_bottom_bar =(LinearLayout)findViewById(R.id.ly_bottom_bar);
        AppUtil.main_cart_num = (TextView) findViewById(R.id.cart_num);
    }

    public void toMainFragment(){
        changeFragment(findViewById(R.id.tv_tab01));
    }

    /**
     * 切换首页片段
     * 在布局里面设置了onClick
     *
     * @param v 点击到的tab
     */
    @SuppressWarnings("unused")
    public void changeFragment(View v) {
        mTransaction = getSupportFragmentManager().beginTransaction();
        String fragmentTag = "";
        //点击前的tab设置selected为false
        tv_tab.setSelected(false);
        //当前tab
        v.setSelected(true);
        //把当前tab存到tv里面
        tv_tab = (TextView) v;
        switch (v.getId()) {
            case R.id.tv_tab01:
                if (index == 0) {
                    return;
                }
                index = 0;
                if (mainFragment == null) {
                    mainFragment = new MainFragment();
                    fragmentList.set(0, mainFragment);
                }
                fragmentTag = "Main";
                tag_list.set(0,fragmentTag);
                break;
            case R.id.tv_tab02:
                if (index == 1) {
                    return;
                }
                index = 1;
                if (classifyFragment == null) {
                    classifyFragment = new ClassifyFragment();
                    fragmentList.set(1, classifyFragment);
                }
                fragmentTag = "classify";
                tag_list.set(1,fragmentTag);
                break;
            case R.id.tv_tab03:
                if(AppUtil.need_to_login(this)){
                    return;
                }
                if (index == 2) {
                    return;
                }
                index = 2;
                if (true) {
                    shoppingCartFragment = new ShoppingCartFragment();
                    fragmentList.set(2, shoppingCartFragment);
                }
                fragmentTag = "cart";
                tag_list.set(2,fragmentTag);
                break;
            case R.id.tv_tab04:
                if(AppUtil.need_to_login(this)){
                    return;
                }
                if (index == 3) {
                    return;
                }
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    fragmentList.set(3, mineFragment);
                }
                fragmentTag = "mine";
                index = 3;
                tag_list.set(3,fragmentTag);
                break;
            default:
                break;
        }

        //0首页mainFragment、1专属limitFragment、2定制dnaTest1Fragment1、3订单orderFragment、4我的mineFragment
        if (!fragmentList.get(index).isAdded()) { // 先判断是否被add过
            mTransaction.add(R.id.content, fragmentList.get(index),tag_list.get(index)); // 隐藏当前的fragment，add下一个到Activity中
        }
        if (index == 0) {//首页
            mTransaction.hide(classifyFragment).hide(mineFragment).hide(shoppingCartFragment).show(mainFragment);
        } else if (index == 1) {//DNA
            mTransaction.hide(mainFragment).hide(mineFragment).hide(shoppingCartFragment).show(classifyFragment);
        } else if (index == 2) {//订单
            mTransaction.hide(mainFragment).hide(mineFragment).hide(classifyFragment).show(shoppingCartFragment);
        } else if (index == 3) {//充值
            mTransaction.hide(mainFragment).hide(classifyFragment).hide(shoppingCartFragment).show(mineFragment);
        }
        Log.e("--------",String.valueOf(index));
        mTransaction.commitAllowingStateLoss();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //【关于性能优化】后期可以拓展低内存或者应用重启的状态反馈
        //到时候参考：http://www.cnblogs.com/android-joker/p/4414891.html
        if (index == 0) {
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //JPushInterface.onResume(this);
        if (getIntent().getStringExtra("from") != null && getIntent().getStringExtra("from").equals("DNATEST")) {
            changeFragment(findViewById(R.id.tv_tab01));
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
    @Override
    protected void onPause() {
        super.onPause();
//        JPushInterface.onPause(this);
    }

    /**
     * 双击退出应用
     */
    private long currentTime = 0;
    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis()-currentTime>1000){
            Toast toast = Toast.makeText(MainActivity.this, "双击退出应用", Toast.LENGTH_SHORT);
            toast.show();
            currentTime=System.currentTimeMillis();
        }else{
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==RESULT_OK){
           if(requestCode==RequstCode.GOTOGOODLIST){
               shoppingCartFragment =new ShoppingCartFragment();
               changeFragment(od_tab);
           }
        }
    }
}
